package cn.task.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.task.po.Task;
import cn.task.po.TaskVo;
import cn.task.po.Taskapplication;
import cn.task.po.Taskmanger;
import cn.task.service.TaskMangerService;
import cn.task.service.TaskService;
import cn.task.service.TaskapplicationService;

@Controller
public class TaskCenterController {
	@Autowired
	private TaskMangerService taskMangerService;
	@Autowired
	private TaskService taskService;
	@Autowired
	private TaskapplicationService taskapplicationService;
	@RequestMapping(value="/showTask")
	public @ResponseBody List<TaskVo> showTask(HttpServletRequest request)
	{
		List<Taskmanger> list=taskMangerService.selectAll();
		StringBuffer sb=new StringBuffer();
		for(Taskmanger taskmanger:list)
		{
			String noreceive=taskmanger.getNoreceive();
			String hadreceive=taskmanger.getHadreceive();
			if(noreceive!=null&&noreceive.length()>0)
			{
				sb.append(noreceive);
			}
			if(hadreceive!=null&&hadreceive.length()>0)
			{
				sb.append(hadreceive);
			}
		}
		List<TaskVo> voList=new ArrayList<TaskVo>();
		String[] IdList=sb.toString().split("#");
		if(IdList!=null&&IdList.length>0)
		{
			for(String str:IdList)
			{
				Task task=taskService.selectTaskById(str);
				TaskVo taskVo=new TaskVo();
				Date begindate=task.getBegindate();
				Date enddate=task.getEnddate();
				Date releasedate=task.getReleasedate();
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
				String startdate=sdf.format(begindate);
				String overdate=sdf.format(enddate);
				String publishdate=sdf.format(releasedate);
				taskVo.setPublishDate(publishdate);
				taskVo.setStartDate(startdate);
				taskVo.setOverDate(overdate);;
				BeanUtils.copyProperties(task, taskVo);
				voList.add(taskVo);
			}
		}
		return voList;
	}
	@RequestMapping(value="/applyTask")
	public @ResponseBody List<String> applyTask(HttpServletRequest request,HttpSession session)
	{
		ArrayList<String> bakList=new ArrayList<String>();
		String useraccount=(String) session.getAttribute("useraccount");
		Taskmanger  taskmanger=taskMangerService.selectByAccount(useraccount);
		String taskId=request.getParameter("taskid");
		if(taskmanger!=null)
		{
			String inapply=taskmanger.getInapply();
			StringBuffer sb=new StringBuffer();
			if(inapply!=null&&inapply.length()>0)
			{
				sb.append(inapply);
				if(inapply.indexOf(taskId)==-1)
				{
					sb.append(taskId+"#");
				}
				else
				{
					bakList.add("true");
				}
			}
			else
			{
				sb.append(taskId+"#");
			}
			taskmanger.setInapply(sb.toString());
			taskMangerService.updateRecord(taskmanger);
		}
		else
		{
			Taskmanger  taskm=new Taskmanger();
			taskm.setUseraccount(useraccount);
			taskm.setInapply(taskId+"#");
			taskMangerService.insertRecord(taskm);
		}
		updateTaskApplict(taskId,useraccount);
		updateReleaser(taskId);
		bakList.add("true");
		return bakList;
	}
	public void updateTaskApplict(String taskId,String useraccount)
	{
		Task task=taskService.selectTaskById(taskId);
		Taskapplication taskapp=new Taskapplication();
		taskapp.setReceiverdate(new Date());
		taskapp.setReceiverid(useraccount);
		taskapp.setReleaserid(task.getReleaserid());
		taskapp.setTaskid(taskId);
		taskapp.setVotesnumber(0);
		//taskapplicationService.selectTaskapplicat(taskId);
		taskapplicationService.insertTaskapplicat(taskapp);
	}
	public void updateReleaser(String taskid)
	{
		Task task=taskService.selectTaskById(taskid);
		String useraccount=task.getReleasername();
		Taskmanger taskmanger=taskMangerService.selectByAccount(useraccount);
		String noreceive=taskmanger.getNoreceive();
		String noreceiveTemp=noreceive.replace(taskid+"#", "");
		String hadreceive=taskmanger.getHadreceive();
		StringBuffer sb=new StringBuffer();
		if(hadreceive!=null&&hadreceive.length()>0)
		{
			sb.append(hadreceive);
			if(hadreceive.indexOf(taskid)==-1)
			{
				sb.append(taskid+"#");
			}
		}
		else
		{
			sb.append(taskid+"#");
		}
		taskmanger.setHadreceive(sb.toString());
		taskmanger.setNoreceive(noreceiveTemp);
		taskMangerService.updateRecord(taskmanger);
	}
}
